package com.mouse.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mouse.common.lang.Result;
import com.mouse.entity.GeneAllele;
import com.mouse.entity.MouseReference;
import com.mouse.service.MouseReferenceService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godson
 * @since 2022-08-07
 */
@RestController
@RequiresAuthentication
@RequestMapping("/admin/mouse-reference")
public class AdminMouseReferenceController {

    @Autowired
    MouseReferenceService mouseReferenceService;

    @GetMapping("/get/")
    public Result select(@Validated @RequestParam String key, @RequestParam String value, @RequestParam int pageNum, @RequestParam int pagesize ){
        IPage<MouseReference> page = new Page<>(pageNum,pagesize);
        IPage<MouseReference> info = mouseReferenceService.getInfoByMap(page, key, value);
        if(info.getSize()==0){
            return Result.fail("无对应记录");
        }
        return Result.succ(info);
    }

    @PostMapping("/createOrUpdate/")
    public Result edit(@Validated @RequestBody MouseReference mouseReference) {

//        Assert.isTrue(false, "公开版不能任意编辑！");

        MouseReference temp = null;
        if(mouseReference.getRid() != null) {
            temp = mouseReferenceService.getById(mouseReference.getRid());

        } else {

            temp = new MouseReference();

        }
//        temp.setCreated(LocalDateTime.now());

        BeanUtil.copyProperties(mouseReference, temp);
        mouseReferenceService.saveOrUpdate(temp);

        return Result.succ("Updated");
    }

    @PostMapping("/delete/{id}")
    public Result delete(@Validated @PathVariable(name = "id") Long id){
        MouseReference mouseReference = mouseReferenceService.getById(id);
        Assert.notNull(mouseReference, "This info doesn't exist");
        mouseReferenceService.removeById(id);
        return  Result.succ("Deleted");
    }
}
